<?php
namespace app\common\logic;

use think\facade\Db;
/**
 * 股票
 */
class Yiwu
{
    /**
     * 状态
     */
    function getStatus(){
        return array(
           0=> '未匹配',
           1=> '交易中',
           2=> '已匹配',
           3=> '已撤销',
        );
    }

    //钱包类型
    public function getMoneyType(){
        $ary = logic('Wallet')->getName();
        unset($ary['xiaofei']);
        unset($ary['score']);
        return  $ary;
    }

    //收款类型
    function getSkType(){
        return array(
            'zfb'=> '支付宝',
            'wx'=> '微信',
        );
    }
    /*
     * 挂单交易
     * $type 1买入 0卖出
     */
    function getTrade($num,$type,$user,$memo,$price,$money_type,$sk_type=''){
        $username=$user['username'];
        $modtime = time();
        $return = array('status'=>0,'msg'=>'');

        if($type==0){//卖出
            //先扣款
            $result=qianbao($user['user_id'],-$num,$money_type,$memo,'数量'.$num.'价格'.$price);
            if(!$result['status']) {
                $return['msg'] = $result['msg'];
                return $return;
            }
        }

        //插入记录
        $huilv = 1;//tpCache('yiwu.stock_huilv');
        $rmb = $price*$huilv*$num;
        $insert = array();
        $insert['user_id'] = $user['user_id'];
        $insert['username'] =$username;
        $insert['price'] = $price;
        $insert['realprice'] = $rmb;
        $insert['huilv'] = $huilv;
        $insert['totalnum'] = $num;
        $insert['synum'] = $num;
        $insert['add_time'] = $modtime;
        $insert['type'] = $type;
        $insert['status'] = 0;
        $insert['order_no'] = date('YmdHis').rand(100000,999999);
        $insert['money_type'] = $money_type;
        $insert['sk_type'] = $sk_type;
        $insert_id = Db::name('yiwu')->insertGetId($insert);
        if(!$insert_id){
           $return['msg'] = '插入匹配记录失败';
           return $return;
        }

        $return['status'] = 1;

        return $return;
    }

    /*
     * 撤单
     * $ids 支持批量
     * $username 为空代表后台
     */
    public function cancel($ids,$username='',$memo=''){
        $return = array('status'=>0,'msg'=>'');
        //查询条件
        $map = array(
            'id'=>array('in',$ids),
            'status'=>0,
        );
        if($username!=''){
            $map['username'] = $username;
        }

        $lists = Db::name('yiwu')->where($map)->field('id,user_id,price,synum,type,order_no')->select();
        if(!$lists){
            $return['msg'] = '没有符合条件的匹配记录';
            return $return;
        }
        foreach($lists as $list){
            if($list['type']==0){
                $result=qianbao($list['user_id'], $list['synum'],'ywmoney','匹配撤销',$memo.'，匹配单号:'.$list['order_no']);
                if(!$result['status']) {
                    $return['msg'] = '匹配撤销失败：'.$result['msg'];
                    return $return;
                }
            }
            $rs = Db::name('yiwu')->update(array('id'=>$list['id'],'status'=>3,'cxtime'=>time()));
            if(!$rs) {
                $return['msg'] = '匹配撤销失败：更新状态失败';
                return $return;
            }
        }
        $return['status'] = 1;
        return $return;
    }

    /*
     * 獲取當前價
     */
    public function getPrice(){
        $price = Db::name('yiwu_price')->order('id desc')->getField('price');
        if(!$price) $price=0;
        return $price;
    }

    /**
     * 卖出
     */
    public function getSell($user,$list){
        $modtime = time();
        $return = array('status'=>0,'msg'=>'');

        $rs =Db::name('yiwu')->where(array('id'=>$list['id']))->save(array('status'=>1));//更新成交易中
        if(!$rs){
            $return['msg'] = '更新匹配记录状态失败';
            return $return;
        }

        //卖出
        if($list['type']==1){

            //根据推荐数量最多卖多少
            $tjnum = $user['tjnum'];
            if($tjnum<5){
                $totalsell = tpCache('yiwu.tjnum'.$tjnum.'_sellnum');//推荐4人最多卖4个
                $ysell = M('YiwuTrade')->where(['seller'=>$user['username'],'status'=>['neq',6],'money_type'=>$list['money_type']])->sum('num');//已经卖了3个
                $ksell = $totalsell-$ysell;
                if($list['synum']>$ksell) return ['status'=>0,'msg'=>'目前您最多能卖出'.$ksell.'个'];
            }

            //判断卖家金额
            $result=qianbao($user['user_id'],-$list['synum'],$list['money_type'],'匹配卖出','单号'.$list['order_no']);
            if(!$result['status']) {
                $return['msg'] = $result['msg'];
                return $return;
            }
            //成交记录
            $cj = [];
            $cj['buyer'] = $list['username'];
            $cj['buyer_user_id'] = $list['user_id'];
            $cj['seller'] = $user['username'];
            $cj['seller_user_id'] = $user['user_id'];
            $cj['buyid'] = $list['id'];
            $cj['add_time'] = $modtime;
            $cj['price'] = $list['price'];
            $cj['realprice'] = $list['price']*$list['huilv']*$list['synum'];
            $cj['num'] = $list['synum'];
            $cj['order_no'] = 'P'.date('Ymd').rand(100,999).$list['id'];
            $cj['fee'] = 0;
            $cj['money_type'] = $list['money_type'];
            $rs = Db::name('yiwu_trade')->insert($cj);
        }else{//买入
            $cj = [];
            $cj['buyer'] = $user['username'];
            $cj['buyer_user_id'] = $user['user_id'];
            $cj['seller'] = $list['username'];
            $cj['seller_user_id'] = $list['user_id'];
            $cj['sellid'] = $list['id'];
            $cj['add_time'] = $modtime;
            $cj['price'] = $list['price'];
            $cj['realprice'] = $list['price']*$list['huilv']*$list['synum'];
            $cj['num'] = $list['synum'];
            $cj['order_no'] = 'P'.date('Ymd').rand(100,999).$list['id'];
            $cj['fee'] = 0;
            $cj['money_type'] = $list['money_type'];
            $rs = Db::name('yiwu_trade')->insert($cj);
        }

        if(!$rs){
            $return['msg'] = '更新成交记录失败';
            return $return;
        }

        $save = array('status'=>2,'end_time'=>$modtime,'synum'=>0);

        $rs =Db::name('yiwu')->where(array('id'=>$list['id']))->save($save);
        if(!$rs){
            $return['msg'] = '更新挂单信息状态失败';
            return $return;
        }

        $return['status'] = 1;
        return $return;
    }
}